﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShiXuLong.Service;
using System.Web.Mvc;
using ShiXuLong.Model;


namespace ShiXuLong.Admin.Controllers
{
    [Authorize]
    // GET: Recommend
    public class RecommendController : Controller
    {
        //ShoesEntities1 db = new ShoesEntities1();
        RecommendPositionService recser = new RecommendPositionService();
        Service.RecommendService shoesser = new Service.RecommendService();
        
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        
        public ActionResult IndexOne()
        {
            return View();
        }
        [HttpGet]//这个不写报错误
        public ActionResult AddInfo()
        {
            return View();
        }
        public ActionResult GetShow()
        {
            var list = shoesser.GetShow();
            return Json(new { code = 0, msg = "", count = 10, data = list }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var shop = shoesser.Edit(id);
            return View(shop);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public ActionResult AddInfo(Shop shop)
        {
            var n = shoesser.AddInfo(shop);
            if (n > 0)
            {
                return Json(new { success = true, msg = "添加成功" });
            }
            else
            {
                return Json(new { success = true, msg = "添加失败" });
            }
        }

        [HttpPost]
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        public ActionResult Edit(Shop shop)
        {
            var n = shoesser.EditInfo(shop);
            if (n > 0)
            {
                return Json(new { success = true, msg = "修改成功" });
            }
            else
            {
                return Json(new { success = true, msg = "修改失败" });
            }
        }
        public ActionResult EditRecommend(int id)
        {
            var rec = shoesser.EditRecommend(id);
            var recTypes = recser.GetReccommend();
            List<SelectListItem> types = new List<SelectListItem>();
            foreach (var item in recTypes)
            {
                types.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.Site });
            }
            ViewData["types"] = types;
            return View(rec);
        }

        [HttpPost]
        public ActionResult EditRecommend(Recommend rec)
        {
            var n = shoesser.EditRecommendInfo(rec);
            if (n > 0)
            {
                return Json(new { success = true, msg = "修改成功" });
            }
            else
            {
                return Json(new { success = true, msg = "修改失败" });
            }
        }
        /// <summary>
        /// 添加推荐
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddRecommendView()
        {
            var recTypes = recser.GetReccommend();
            var rectypes = recser.GetShopId();
            //List<SelectListItem> types = new List<SelectListItem>();
            //foreach (var item in recTypes)
            //{
            //    types.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.Site });
            //}
            // ViewData["types"] = types;
            ViewBag.types = recTypes;
            ViewBag.bag = rectypes;
            return View();
  
        }
        public ActionResult AddRecommend(Recommend recommend)
        {
            var n = shoesser.GetAddReccommend(recommend);
            if (n > 0)
            {
                return Json(new { success = true, msg = "添加成功" });
            }
            else
            {
                return Json(new { success = true, msg = "添加失败" });
            }
        }
        /// <summary>
        /// 推荐显示
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRecommend(int limit, int page)
        {
            int count = 0;

            var list = shoesser.GetRecommed(limit, page, out count);
            return Json(new { code = 0, msg = "", count = count, data = list }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetRecommendTest(int limit, int page, int shopId = 0, int recId = 0)
        {
            int count = 0;

            var list = shoesser.GetRecommedTest(limit, page, shopId, recId, out count);
            return Json(new { code = 0, msg = "", count = count, data = list }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetRecommendView()
        {
            //var recTypes = recser.GetShopId();
            //List<SelectListItem> types = new List<SelectListItem>();
            //foreach (var item in recTypes)
            //{
            //    types.Add(new SelectListItem { Value = item.Id.ToString(), Text = item.ShopId });
            //}
            //ViewData["types"] = types;


            //List<SelectListItem> types = new List<SelectListItem>();
            //foreach (var item in recTypes)
            //{
            //    types.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.Site });
            //}
            // ViewData["types"] = types;


            //var recTypes = recser.GetReccommend();
            //ViewBag.types = recTypes;
            return View();
           


        }
        /// <summary>
        /// 显示推荐位置
        /// </summary>
        /// <returns></returns>
        public ActionResult RecPosition(int limit, int page, string site)
        {
            int count = 0;
            var list = shoesser.RecPosition(limit, page, site, out count);
            return Json(new { code = 0, msg = "", count = count, data = list }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult RePositionView()
        {
            return View();
        }
        /// <summary>
        /// 添加推荐位置
        /// </summary>
        /// <returns></returns>
        /// 
        //这个不能【httpget】要不然报错 对控制器的操作不明确
        public ActionResult AddRecPosition(RecPosition rec)
        {
            var n = shoesser.AddRecPositions(rec);
            if (n > 0)
            {
                return Json(new { success = true, msg = "添加成功" });
            }
            else
            {
                return Json(new { success = true, msg = "添加失败" });
            }
        }
        [HttpGet]
        public ActionResult AddRecPosition()
        {
            return View();
        }
        /// <summary>
        /// 删除推荐(有商品进行提示)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult DelReposition(int id)
        {
            var list = shoesser.DelPosition(id);
            if (list > 0)
            {
                return Json(new { success = true, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { success = false, msg = "已有推荐" }, JsonRequestBehavior.AllowGet);
            }

            //return Json(list,JsonRequestBehavior.AllowGet);
        }
        public ActionResult DelRecommend(int id)
        {
            var list = shoesser.DelRecommend(id);
            if (list > 0)
            {
                return Json(new { success = true, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}